package phase1;

import java.util.*;
import java.io.BufferedInputStream;
import java.io.DataInputStream;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;

import java.io.IOException;
import java.net.URL;

public class Dictionary {

	// private static final char A = 0;
	private char index;

	private Vector<String> dictionaryWords;
	public int limit;
	
	//default constructor
	public Dictionary() {
		index = 0;
		dictionaryWords = new Vector<String>();
		limit = 0;
	}
	
	//copy constructor
	public void setIndex(char index) {
		this.index = index;
	}
	
	//get all the words from a certain text file
	public Vector<String> getAll() {
		File file = new File("wordsDB/"+index+".txt");
		FileInputStream fis = null;
		BufferedInputStream bis = null;
		DataInputStream dis = null;

		BufferedReader reader = null;

        try{
                reader = new BufferedReader(new FileReader(file));
                String temp = null;
                while ((temp = reader.readLine()) != null){
                	dictionaryWords.add(temp);
                }
                reader.close();
        }catch(Exception e){
                e.printStackTrace();
        	System.out.println("Can't find file");
        }finally{
                if (reader != null){
                        try{
                                reader.close();
                        }catch(Exception e){
                                e.printStackTrace();
                        }
                }
        }
		return dictionaryWords;
	}
	
	// find the maximum length in three lists starting by three characters in the first block
	public int getMaxLength(char a, char b, char c){
		
		Vector<String> tempDictionaryWords = new Vector<String>();
		
		if (a != ' '){
			setIndex(a);
			tempDictionaryWords.addAll(getAll());
		}
		if (b != ' '){
			setIndex(b);
			tempDictionaryWords.addAll(getAll());
		}
		if (c != ' '){
			setIndex(c);
			tempDictionaryWords.addAll(getAll());
		}
				
		for(int i=0; i<tempDictionaryWords.size(); i++){
			if (tempDictionaryWords.get(i).length()>limit)
				limit = tempDictionaryWords.get(i).length();
		}
		return limit;
	}
}
